﻿using AutoMapper;
using Common.Result;
using Interface;
using IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Dto.Employee;
using Model.Entitys;
using Service;

namespace TakeawayWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        private readonly IEmployeeService _employeeService;
        private readonly ILogger<EmployeeController> _logger;
        private readonly ICustomJWTService _jWTService;
        public EmployeeController(IEmployeeService employeeService
            ,ILogger<EmployeeController> logger
            , ICustomJWTService jWTService)
        {
            _employeeService = employeeService;
            _logger = logger;
            _jWTService = jWTService;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginRes"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public async Task<ApiResult> Login(EmployeeLoginReq loginRes)
        {
            if (string.IsNullOrEmpty(loginRes.Username) || string.IsNullOrEmpty(loginRes.Password)){
                return ResultHelper.Error("用户名或密码不能为空");
            }
            Employee dbEmployee = await _employeeService.LoginAsync(loginRes);
            //返回对象为空登录失败
            if(dbEmployee==null ) return ResultHelper.Error("登录失败");
            //判断账号状态
            if(dbEmployee.Status == 0) return ResultHelper.Error("账号已禁用");
            var token = await _jWTService.GetTokenAsync(dbEmployee);
            _logger.LogInformation($"用户dbEmployee:{dbEmployee.Id}\t登录系统");
            return ResultHelper.Success(token);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet("page")]
        public async Task<ApiResult> GetEmployeePage(int page, int pageSize, string? name)
        {
            var res= await _employeeService.GetEmployeePageAsync(page, pageSize, name);
            return ResultHelper.Success(res);
        }

        [Authorize]
        [HttpDelete("{id}")]
        public async Task<ApiResult> DeleteEmployeeById(long id)
        {
            var res= await _employeeService.DeleteEmployeeByIdAsync(id);
            if (res)
            {
                return ResultHelper.Success("删除成功");
            }
            return ResultHelper.Error("删除失败");
        }

        [Authorize]
        [HttpPut]
        public async Task<ApiResult> EditEmployee(EmployeeEdit employeeEdit)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            var res= await _employeeService.EditEmployeeAsync(employeeEdit, userId);
            if (res)
            {
                return ResultHelper.Success("修改成功");
            }
            return ResultHelper.Error("修改失败");
        }
        [Authorize]
        [HttpGet("{id}")]
        public async Task<ApiResult> GetEmployeeById(long id)
        {
            return ResultHelper.Success(await _employeeService.GetEmployeeByIdAsync(id));  
        }
        [Authorize]
        [HttpPost]
        public async Task<ApiResult> SaveEmployee(EmployeeAdd employeeAdd)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            var res= await _employeeService.SaveEmployeeAsync(employeeAdd, userId);
            if (res) return ResultHelper.Success("添加成功");
            return ResultHelper.Error("添加失败");
        }
    }
}
